package PartD;

import java.net.*;
import java.util.concurrent.Semaphore;

public class EchoServer {

	private static final int MAX_CONNECTIONS = 2;
	private static final int PORT = 6789;

	public static void main(String args[]) throws Exception {

		Semaphore sem = new Semaphore(MAX_CONNECTIONS, true);

		ServerSocket welcomeSocket = new ServerSocket(PORT);

		while (true) {
			// acquire a permit before accepting a connection
			sem.acquire();
			Socket socket = welcomeSocket.accept();
			// pass the semaphore to the Request objects
			Request request = new Request(socket, sem);
			Thread thread = new Thread(request);
			thread.start();
		}
	}
}